package com.xh.easyretry;

import com.xh.easyretry.annotations.TimeAvoidance;
import com.xh.easyretry.annotations.XHRetry;
import com.xh.easyretry.common.result.CommonResult;
import com.xh.easyretry.common.result.CommonResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CommonServiceImpl {

    private final String username = "yixinhai";

    @XHRetry(retryCount = 4, retryStrategy = "sync", lastFailedListener = "retryWithLastFailed",
            timeAvoidance = @TimeAvoidance(avoidance = "gradient", time = 500, multiplier = 1.5))
    public CommonResult<String> retryWithResult() {
        log.info("act=retryWithResult msg=retry start username={}", username);
        return CommonResult.fail(CommonResultCode.NEED_RETRY_FAIL, "need retry fail");
    }

    public void retryWithResultFirstRetry() {
        log.info("act=retryWithResultFirstRetry msg=custom first retry function");
    }

    public void retryWithResultRetryFailed() {
        log.info("act=retryWithResultRetryFailed msg=custom retry failed function");
    }

    public void retryWithLastFailed() {
        log.info("act=retryWithLastFailed msg=custom retry last failed function");
    }

    @XHRetry(retryCount = 4, retryStrategy = "async", lastRetryListener = "retryWithExceptionLastRetry")
    public int retryWithException() {
        log.info("act=retryWithException msg=retry start username={}", username);
        throw new IllegalArgumentException("retryWithException");
    }

    public void retryWithExceptionLastRetry() {
        log.info("act=retryWithExceptionLastRetry msg=custom last retry function");
    }

}
